/*
 * @Author: huangtianyang 916072572@qq.com
 * @Date: 2023-05-18 09:16:20
 * @LastEditors: huangtianyang
 * @LastEditTime: 2023-08-31 15:11:28
 * @FilePath: \cxl-admin\src\pages\finance\refund\list\index.ts
 */
import { selectCompany } from '@/apis/company/audit'
import { refundList, refundParamsRefund } from '@/apis/finance/refund'
import { BaseData, BaseMethod } from '@/utils/basc-data'
import { reactive, ref } from 'vue'
import * as tableConfig from './config.json'

export class Refund extends BaseData implements BaseMethod {
    //搜索数据
    searchData = reactive({
        name: '0',
        status: '1'
    })
    //操作弹窗数据
    operateDialogData = reactive({
        visible: false,
        title: 'this is a dialog',
        data: '',
        type: 1, //1正常2退款中3已退款
        id: ''
    })
    companyId = ref('')
    companyName = ref('')
    searchCompany = reactive({
        width: '180px',
        name: '',
        disabled: false
    })
    loading = ref(false)
    //定义公司选择器
    companyOptions: any[] = reactive([])
    constructor() {
        super(tableConfig)
        this.getData()
    }

    isClick = false
    onOperation = (arg?: any) => {
        const { type, data } = arg
        if (!this.isClick) {
            this.isClick = true
            this.operateDialogData.visible = true
            this.operateDialogData.type = type === 'refund' ? 1 : 2
            this.operateDialogData.title = type === 'refund' ? '退款' : '查看'
            this.operateDialogData.data = data
        }
    }
    /**
     * 监听用户输入的公司名，搜索
     * @param query
     */
    remoteMethod = (query: string) => {
        console.log(query)
        if (query) {
            this.loading.value = true
            this.getCompanyList(query, () => {
                this.loading.value = false
            })
        }
    }

    /**
     * 监听选择的公司
     * @param res
     */
    onSelectCompany = (res: any) => {
        console.log(res)
        this.companyId.value = res
        this.searchCompany.name = res
    }
    /**
     * 获取公司
     * @param name
     * @param callback
     */
    getCompanyList(name = '', callback = () => {}) {
        selectCompany({ current: 1, size: 10, companyParam: name }).then(res => {
            if (res.data.value === 200) {
                this.companyOptions.length = 0
                res.data.data.records.map((item: any) => {
                    this.companyOptions.push({ value: item.companyId, label: `${item.companyName}` })
                })
                // this.pageData.table.length = 0
                // this.pageData.table.push(...res.data.data.records)
                // this.pageData.pagination.total = res.data.data.total
                callback()
            }
        })
    }
    /**
     * 拉取数据
     */
    getData = () => {
        const obj: refundParamsRefund = {
            current: this.pageData.pagination.pageNo,
            size: this.pageData.pagination.pageSize
        }

        if (this.companyId.value) {
            obj.companyId = this.companyId.value
        }

        if (Number(this.searchData.status)) {
            obj.refundStatus = Number(this.searchData.status)
        }

        refundList(obj).then(res => {
            if (res.data.value === 200) {
                this.pageData.table.data.length = 0
                this.pageData.pagination.total = res.data.data.total
                res.data.data.records.forEach((item: any) => {
                    item.usedFreeCount = item.usedFreeCount ? item.usedFreeCount : 0
                    this.pageData.table.data.push(item)
                })
            }
        })
    }
    /**
     * 监听搜索
     */
    onSearch = (res: any) => {
        this.searchData.status = res.status
        this.companyId.value = res.companyId
        this.getData()
    }
    /**
     * 监听点击重置按钮
     */
    onReset = () => {
        this.searchData.name = ''
        this.searchData.status = '0'
        this.pageData.pagination.pageNo = 1
        this.companyId.value = ''
        this.searchCompany.name = ''
        this.getData()
    }
    /**
     * 监听分页发生改变
     */
    onPaginationChange = (index: number) => {
        this.pageData.pagination.pageNo = index
        this.getData()
    }
    /**
     * 监听选择每页显示个数
     */
    onPaginationSize = (size: number) => {
        this.pageData.pagination.pageNo = 1
        this.pageData.pagination.pageSize = size
        this.getData()
    }

    /**
     * 监听操作弹窗隐藏
     */
    onOperateDialogVisible = () => {
        this.isClick = false
        this.operateDialogData.visible = false
        this.getData()
    }
}
